//
//  oldActivityViewController.m
//  laonianbang
//
//  Created by 勇拓 李 on 18/4/3.
//  Copyright © 2018年 zaiyou. All rights reserved.
//

#import "oldActivityViewController.h"
#import "oldActivityTableViewCell.h"
#import "oldTopChooseScrollView.h"
#import "oldActivityDetailViewController.h"

@interface oldActivityViewController ()<TopChooseDelegate>
@property (nonatomic, copy)NSArray *dataArray;
@property (nonatomic, strong)oldTopChooseScrollView *topScrollView;

@property (nonatomic, assign)BOOL isNew;
@end

static NSString *const identifier = @"oldActivityTableViewCell";

@implementation oldActivityViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initView];
    [self initData];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - lazyLoad
- (oldTopChooseScrollView *)topScrollView
{
    if (!_topScrollView) {
        _topScrollView = [[[NSBundle mainBundle] loadNibNamed:@"oldTopChooseScrollView"
                                                        owner:self
                                                      options:nil] objectAtIndex:0];
        
        _topScrollView.delegate = self;
        [_topScrollView changeType];
        
        NSMutableArray *titleArray = [NSMutableArray array];
        [titleArray addObject:@"进行中"];
        [titleArray addObject:@"已结束"];
        
        [_topScrollView initWithList:titleArray width:SCREEN_WIDTH/2];
    }
    
    return _topScrollView;
}

#pragma mark - initView
- (void)initView
{
    [self rigisterCell:identifier];
    _isNew = YES;
    self.navigationItem.title = _activity.title;
    self.view.backgroundColor = [UIColor whiteColor];
}

#pragma mark - initData
- (void)initData
{
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(refresh)];
    self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMore)];
    [self.tableView.mj_header beginRefreshing];
}

- (void)refresh
{
    NSMutableDictionary *params = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"1", @"pageNum", @"10", @"pageSize", _activity.aid, @"aid", nil];
    
    [self loadData:params ifResetPageNo:YES];
}

- (void)loadMore
{
    NSString *pageNum = [NSString stringWithFormat:@"%ld", (long)pageNo + 1];
    
    NSMutableDictionary *params = [NSMutableDictionary dictionaryWithObjectsAndKeys:pageNum, @"pageNum", @"10", @"pageSize", _activity.aid, @"aid", nil];
    
    [self loadData:params ifResetPageNo:NO];
}

- (void)loadData:(NSDictionary *)params ifResetPageNo:(BOOL)reset
{
    WeakSelf
    
    _topScrollView.userInteractionEnabled = NO;
    
    [OLDNetWork httpRequestWithPostPut:[self getUrl] params:params success:^(id data) {
        if (reset) {
            pageNo = 1;
            weakSelf.dataArray = [NSArray array];
            [weakSelf.tableView.mj_header endRefreshing];
        }
        else
        {
            pageNo ++;
            [weakSelf.tableView.mj_footer endRefreshing];
        }
        
        [weakSelf initDataSource:data];
        [weakSelf.tableView reloadData];
        weakSelf.topScrollView.userInteractionEnabled = YES;
        
    } error:^(NSString *error) {
        if (reset) {
            [weakSelf.tableView.mj_header endRefreshing];
        }
        else
        {
            [weakSelf.tableView.mj_footer endRefreshing];
        }
        [oldHub warningUser:error];
        weakSelf.topScrollView.userInteractionEnabled = YES;
    }];
}

- (void)initDataSource:(id)data
{
    NSMutableArray *array = [_dataArray mutableCopy];
    
    for (int i = 0; i < [data count]; i++) {
        oldActivity *activity = [[oldActivity alloc] initWithDictionary:data[i]];
        
        if(!_isNew)
        {
            activity.status = @"报名已结束";
        }
        else
        {
            activity.status = @"报名进行中";
        }
        
        [array addObject:activity];
    }
    
    _dataArray = array;
}

- (NSString *)getUrl
{
    if (_isNew) {
        return URL_GetComingActivityList;
    }
    else
    {
        return URL_GetHistoryActivityList;
    }
}

#pragma mark topScrollViewDelegate

- (void)topScrollViewChoosed:(NSUInteger)index
{
    if (!index) {
        _isNew = YES;
    }
    else
    {
        _isNew = NO;
    }
    
    [self.tableView.mj_header beginRefreshing];
}


#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return [_dataArray count];
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    oldActivityTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier forIndexPath:indexPath];
    
    if (cell == nil) {
        cell = [[oldActivityTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }
    
    cell.activity = _dataArray[indexPath.row];
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    oldActivityDetailViewController *vc = [oldActivityDetailViewController new];
    vc.activity = _dataArray[indexPath.row];
    vc.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:vc animated:YES];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 141;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 52)];
    
    [view addSubview:self.topScrollView];
    
    return view;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 52.f;
}

@end
